import javax.microedition.location.Coordinates;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;

import net.sf.microlog.core.Logger;
import net.sf.microlog.core.LoggerFactory;
import net.sf.microlog.core.PropertyConfigurator;

public class Locator implements LocationListener {
	
	private static final Logger logger = LoggerFactory.getLogger(Locator.class);
	
	private PlayerManager pm;

	public Locator() throws LocationException {

//		PropertyConfigurator.configure("/microlog.properties"); //loads the configuration file
		
		LocationProvider lp;
		Criteria crt = new Criteria();
		
		crt.setHorizontalAccuracy(200);
		lp = LocationProvider.getInstance(crt);
		lp.setLocationListener(this, 10, -1, -1);
		
		pm = PlayerManager.getPlayerManager();
	}

	public void locationUpdated(LocationProvider lp, Location location) {
		
		Coordinates coord = location.getQualifiedCoordinates();
		
		double latitude = coord.getLatitude();
		double longitude = coord.getLongitude();
		
//		MyLogger.debug("New location: " + coord.getLatitude() + " " + coord.getLongitude());
		logger.debug("New location: " + latitude + " " + longitude);
		
		if(latitude > 0 && latitude < 50) {
			pm.setMediaToPlay(PlayerManager.MEDIA1);
		}
		else {
			pm.setMediaToPlay(PlayerManager.MEDIA2);
		}
	}

	public void providerStateChanged(LocationProvider arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
	
}
